import pymysql as pymysql


class MySql(object):
    def mysql(sql):
        # 连接数据库
        hostvalue = 'localhost'
        uservalue = 'root'
        passwordvalue = '123456'
        dbvalue = 'test'
        portvalue = 3306
        connection = pymysql.connect(host=hostvalue, user=uservalue, password=passwordvalue, db=dbvalue, port=portvalue,
                                     charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor)

        # 通过cursor创建游标
        cursor = connection.cursor()
        # 创建sql 语句，并执行
        sqlvalue = sql  # "select * from user"
        cursor.execute(sqlvalue)
        result = cursor.fetchone()  # 查询数据库单条数据
        # result = cursor.fetchall() #查询数据库多条数据
        # 提交sql
        connection.commit()
        return result

    if __name__ == '__main__':
        a = mysql(sql="select * from user")
        print(a)
        #b = mysql(sql="select name from user where id='213'")
        #print(b)
        #mysql(sql="update user set name='哈哈' where studentid=23")